package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.model.Project;
import com.nefu.softlab.xjwc.service.ProjectService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.JsonUtil;
import com.nefu.softlab.xjwc.utils.LoggerUtil;
import com.nefu.softlab.xjwc.utils.XJWCException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author : Ar1es
 * @date : 2020/2/8
 * @since : Java 8
 */
@RestController
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@RequiresRoles("admin")
@Slf4j
public class ProjectApi {

    private final ProjectService projectService;
    private final LoggerUtil logger;

    @Autowired
    public ProjectApi(ProjectService projectService, LoggerUtil logger) {
        this.projectService = projectService;
        this.logger = logger;
    }


    @RequiresRoles("admin")
    @PostMapping(value = "/project")
    public JSONResult insertProject(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        return JSONResult.ok(projectService.insertProject(project));
    }

    @RequiresRoles("doctor")
    @PostMapping("/pro")
    public JSONResult selectAllProject(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectAllProject : " + JsonUtil.getJsonString(project));
        return JSONResult.ok(projectService.selectAllProject(project));
    }

    @RequiresRoles("doctor")
    @GetMapping("/project/{keyword}")
    public JSONResult selectProjectByKeyword(@PathVariable String keyword) throws XJWCException {
        if (StringUtils.isEmpty(keyword)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectProjectByKeyword : keyword = " + keyword );
        return JSONResult.ok(projectService.selectProjectByKeyword(keyword));
    }

    @RequiresRoles("admin")
    @GetMapping("/project/getId/{activityId}/{name}")
    public JSONResult selectProIdByActivityId(@PathVariable("activityId") Integer activityId, @PathVariable("name") String name) throws XJWCException {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(activityId)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectProIdByActivityId : ");
        return JSONResult.ok(projectService.selectProIdByActivityId(activityId, name));
    }

    @RequiresRoles("admin")
    @PutMapping("/project")
    public JSONResult updateProject(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("PUT updateProject : " + JsonUtil.getJsonString(project));
        return JSONResult.ok(projectService.updateProject(project));
    }


    @RequiresRoles("admin")
    @DeleteMapping("/project")
    public JSONResult deleteProject(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("DELETE deleteProject : " + JsonUtil.getJsonString(project));
        return JSONResult.ok(projectService.deleteProject(project));
    }


    @RequiresRoles("admin")
    @DeleteMapping("/project/delete")
    public JSONResult deleteProject1(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("DELETE deleteProject : " + JsonUtil.getJsonString(project));
        return JSONResult.ok(projectService.deleteProject1(project));
    }

    @RequiresRoles("admin")
    @PostMapping("/sort")
    public JSONResult selectSortProject(@RequestBody Project project) throws XJWCException {
        if (ObjectUtils.isEmpty(project)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("POST selectSortProject : " + JsonUtil.getJsonString(project));
        return JSONResult.ok(projectService.selectSortProject());
    }


    @RequiresRoles("doctor")
    @GetMapping("/detail")
    public JSONResult selectProjectDetail(@RequestParam("id") Integer projectId) throws XJWCException {
        if (ObjectUtils.isEmpty(projectId)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");

        }
        logger.info("GET selectProjectDetail : " + JsonUtil.getJsonString(projectId));

        return JSONResult.ok(projectService.selectProjectDetail(projectId));
    }



    @RequiresRoles("doctor")
    @GetMapping("/project/user")
    public JSONResult getProjectByUserAndActivity(@RequestParam("userId") String userId, @RequestParam("activityId") Integer activityId) throws XJWCException {

        if (StringUtils.isEmpty(userId) || ObjectUtils.isEmpty(activityId)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }

        logger.info("GET getProjectByUserAndActivity : " + userId + " " + activityId);

        return JSONResult.ok(projectService.getProjectByUserAndActivity(userId, activityId));

    }
}
